//
//  HotMoreViewController.m
//  ThePrivateKitchen
//
//  Created by lirui on 16/1/22.
//  Copyright © 2016年 www.lanou3g.com蓝鸥科技. All rights reserved.
//

#import "HotMoreViewController.h"


#define WIDTH [UIScreen mainScreen].bounds.size.width /375
#define HEIGHT [UIScreen mainScreen].bounds.size.height /667
@interface HotMoreViewController ()<UITableViewDataSource,UITableViewDelegate,UIScrollViewDelegate>

@property(nonatomic, retain)UITableView *tableView;
@property(nonatomic ,retain)NSMutableArray *dataArr; //解析的数据
@property(nonatomic ,retain)UITableView  *nTableView;
@property(nonatomic ,retain)UIScrollView *sv;//底部视图
@property(nonatomic, retain)NSMutableArray *nArr;
@property(nonatomic ,retain)UISegmentedControl *seg;
@property(nonatomic, retain)UITableView *zhuantiTableview;
@property(nonatomic ,retain)NSMutableArray *zArr; //专题的数据请求
@property(nonatomic,retain)UITableView *hotFoodTableview; //热门菜的tableview
@property (nonatomic, assign) NSInteger index;
@property (nonatomic, assign) BOOL isRefrsh;
@property(nonatomic, assign)NSInteger size;


@end

static NSInteger i = 1;
static NSInteger j = 6;
@implementation HotMoreViewController

- (void)dealloc
{
    [_tableView release];
    [_dataArr release];
    [_nTableView release];
    [_sv release];
    [_nArr release];
    [_seg release];
    [_zhuantiTableview release];
    [_zArr release];
    [_hotFoodTableview release];
    [super dealloc];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [ UIColor whiteColor];
    self.navigationController.navigationBar.translucent = NO;
    
    self.nArr = [NSMutableArray array];
    self.dataArr = [ NSMutableArray array];
    
    //重写左按钮
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithImage:[[UIImage imageNamed:@"left@3x"]imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] style:UIBarButtonItemStylePlain target:self action:@selector(leftAction:)];
   

    
      /**热门推荐*/
if (self.tag == 401)
         {
        
//    [self preparedata];
//    [self preparedata1];
    [self creatBottom];
    [self creatTableview];
    [self creatView];
    [self creatSeg];
   [self loadMBProgress];
         }
    /**专题*/
    else if (self.tag == 402)
    {     [self preparedata2];
        [self creatzhaungti];
         [self loadMBProgress];
        self.navigationItem.title = @"专题列表";
    }
    /**本周热门菜*/
    else if(self.tag == 403)
    {
        [self preparedata3];
        self.navigationItem.title = @"排行榜10道菜";
        [self creathot_food];
         [self loadMBProgress];
    }
}
-(void)leftAction:(UIBarButtonItem *)left
{
    [self.navigationController popViewControllerAnimated:YES];
    
}
//热门推荐的数据
-(void)preparedata

{
    
    NSString *str =[NSString stringWithFormat:@"http://api.izhangchu.com/?methodName=HomeMore&page=%ldf&size=%ld&token=0&type=1&user_id=0&version=4.1",(long)i,(long)j];
    [LRAFNetwork postUrlString:str body:nil response:JSON bodyStyle:RequestJSON requestHeadFile:nil success:^(NSURLSessionDataTask *task, id responseObject) {
        if (self.isRefrsh) {
            [self.dataArr removeAllObjects];
        }
        
        NSArray *arr =  responseObject[@"data"][@"data"];
        [self.dataArr addObjectsFromArray:[ShipuModel modelHanderWithArray:arr]];
        [_tableView reloadData];
        [self.hud removeFromSuperview];
       
        [self.tableView.mj_header endRefreshing];
       
        [self.tableView.mj_footer endRefreshing];

    }
    failure:^(NSURLSessionDataTask *task, NSError *error) {
        
    }];
    
    
    
}
//热门推荐里新品的请求
-(void)preparedata1
{ NSString *str =[NSString stringWithFormat:@"http://api.izhangchu.com/?methodName=HomeMore&page=%ld&size=6&token=0&type=2&user_id=0&version=4.1",(long)i];
    [LRAFNetwork postUrlString:str body:nil response:JSON bodyStyle:RequestJSON requestHeadFile:nil success:^(NSURLSessionDataTask *task, id responseObject) {
  
        if (self.isRefrsh) {
            [self.nArr removeAllObjects];
        }
        NSArray *arr =  responseObject[@"data"][@"data"];
        
        [self.nArr addObjectsFromArray: [ShipuModel modelHanderWithArray:arr]];
      
        [_nTableView reloadData];
        [self.hud removeFromSuperview];
        [self.nTableView .mj_header endRefreshing];
        [self.nTableView.mj_footer endRefreshing];
    
    }
     
     
    failure:^(NSURLSessionDataTask *task, NSError *error) {
        
    }];
    
    
}
//专题的数据请求
-(void)preparedata2
{ NSString *str =[NSString stringWithFormat:@"http://api.izhangchu.com/?methodName=TopicList&page=%ld&size=%ld&token=0&type=1&user_id=0&version=4.1",(long)i,(long)j];
    [LRAFNetwork postUrlString:str body:nil response:JSON bodyStyle:RequestJSON requestHeadFile:nil success:^(NSURLSessionDataTask *task, id responseObject) {
        if (_isRefrsh) {
            [_zArr removeAllObjects];
        }
        NSArray *arr =  responseObject[@"data"][@"data"];
        self.zArr = [ShipuModel modelHanderWithArray:arr];
        [_zhuantiTableview reloadData];
        [self.hud removeFromSuperview];
        [self.zhuantiTableview .mj_header endRefreshing];
        
        [self.zhuantiTableview.mj_footer endRefreshing];
        if (j > 20) {
            
            
            [self.zhuantiTableview.mj_footer endRefreshingWithNoMoreData];
        }
        
   

     
    } failure:^(NSURLSessionDataTask *task, NSError *error) {
        
    }];
    
    
}
//热门菜的数据请求
-(void)preparedata3
{ NSString *str = @"http://api.izhangchu.com/?methodName=TopList&page=1&size=10&token=0&user_id=0&version=4.1";
    [LRAFNetwork postUrlString:str body:nil response:JSON bodyStyle:RequestJSON requestHeadFile:nil success:^(NSURLSessionDataTask *task, id responseObject) {
        NSArray *arr =  responseObject[@"data"][@"data"];
        self.dataArr = [ShipuModel modelHanderWithArray:arr];
        [_hotFoodTableview reloadData];
        [self.hud removeFromSuperview];
    } failure:^(NSURLSessionDataTask *task, NSError *error) {
        
    }];
    
    
}
//创建热门菜的
-(void)creathot_food

{
    self.hotFoodTableview = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, WIDTH *375, HEIGHT *667) style:UITableViewStylePlain];
   _hotFoodTableview.dataSource = self;
   _hotFoodTableview.delegate = self;
   _hotFoodTableview. backgroundColor =[  UIColor whiteColor];
    [_hotFoodTableview registerClass:[SancanTableViewCell class] forCellReuseIdentifier:@"foodcell"];
    [self.view addSubview:_hotFoodTableview];
    [_hotFoodTableview release];
}
//创建专题的tableview
-(void)creatzhaungti
{
    self.zhuantiTableview = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, WIDTH *375, HEIGHT *667) style:UITableViewStylePlain];
    _zhuantiTableview.dataSource = self;
    _zhuantiTableview.delegate = self;
    _zhuantiTableview.backgroundColor =[  UIColor whiteColor];
    [_zhuantiTableview registerClass:[Head_2TableViewCell class] forCellReuseIdentifier:@"zhuanticell"];
    [self.view addSubview:_zhuantiTableview];
    [_zhuantiTableview release];
    
    
    MJRefreshGifHeader *header = [MJRefreshGifHeader headerWithRefreshingBlock:^{
        //赋初值
        i = 1;
        self.isRefrsh = YES;
        
        [self preparedata2];
    }];
    self.zhuantiTableview.mj_header = header;
    [self.zhuantiTableview.mj_header beginRefreshing];
    MJRefreshAutoNormalFooter *footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        i++;
        j+=6;
        self.isRefrsh = NO;
        [self preparedata2];
    }];
    self.zhuantiTableview.mj_footer = footer;

}
-(void)creatBottom
{
    // 1.创建
    self.sv = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, WIDTH*375, HEIGHT*667)];
    // 2.设置
    
    _sv.backgroundColor = [UIColor yellowColor];
    // 3.添加父视图
    [self.view addSubview:_sv];
    // 4.内存
    [_sv release];
    _sv.delegate = self;
    /**< contentSize 内容大小 */
    _sv.contentSize = CGSizeMake(375 * 2*WIDTH, 0);
    /**< pagingEnbaled 整页翻动 */
    _sv.pagingEnabled = YES;
    
}
//创建热门
-(void)creatView
{
    self.tableView = [[ UITableView alloc] initWithFrame:CGRectMake(0, 0, WIDTH *375, HEIGHT *667) style:UITableViewStylePlain];
    _tableView.dataSource = self;
    _tableView.delegate = self;
    _tableView.backgroundColor= [ UIColor whiteColor];
    _tableView.contentInset = UIEdgeInsetsMake(0, 0, WIDTH*100, 0);

    [_tableView registerClass:[SancanTableViewCell class] forCellReuseIdentifier:@"morecell"];
    [self.sv addSubview:_tableView];
    //下拉刷新
    MJRefreshGifHeader *header = [MJRefreshGifHeader headerWithRefreshingBlock:^{
        //赋初值
        i = 1;
        self.isRefrsh = YES;

        [self preparedata];
        
    }];
    self.tableView.mj_header = header;
      [self.tableView.mj_header beginRefreshing];
    //上拉加载
    MJRefreshAutoNormalFooter *footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        i++;
        j+=6;
        self.isRefrsh = NO;
        [self preparedata];
    }];
    self.tableView.mj_footer = footer;
}
//创建新品tableview
-(void)creatTableview
{
    self.nTableView = [[ UITableView alloc] initWithFrame:CGRectMake(WIDTH *375, 0, WIDTH *375, HEIGHT *667) style:UITableViewStylePlain];
   _nTableView.dataSource = self;
  _nTableView.delegate = self;
   _nTableView.backgroundColor = [UIColor whiteColor];
    [_nTableView registerClass:[ListdetailTableViewCell class] forCellReuseIdentifier:@"newcell"];
    [self.sv addSubview:_nTableView];
    
    MJRefreshGifHeader *header = [MJRefreshGifHeader headerWithRefreshingBlock:^{
        //赋初值
        i = 1;
        self.isRefrsh = YES;
        
        [self preparedata1];

       
    }];
    self.nTableView.mj_header = header;
    [self.nTableView.mj_header beginRefreshing];
    //上拉加载
    MJRefreshAutoNormalFooter *footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        i++;
        self.isRefrsh = NO;
        [self preparedata1];
    }];
    self.nTableView.mj_footer = footer;

    

    
}
#pragma mark -协议方法
//判断不同的显示界面
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{//热门推荐中的推荐 和热门菜
    if (tableView == self.tableView || tableView == self.hotFoodTableview) {
        
    
    return _dataArr.count;
}
    //热门推荐里的新品
 else if (tableView == self.nTableView)
 {
     return _nArr.count;
 }
    //专题
 else if(tableView == _zhuantiTableview)
 {
     
     return _zArr.count;
 }
 else
         return  0;
 
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

{  //热门推荐的热门
    if(tableView == _tableView){
    SancanTableViewCell *cell =[tableView dequeueReusableCellWithIdentifier:@"morecell" forIndexPath:indexPath];
    ShipuModel *m = _dataArr[indexPath.row];
    cell.name.text = m.title;
    cell.content.text = m.des;
    [cell.img sd_setImageWithURL:[NSURL URLWithString:m.image]];
    return cell;
}  //热门推荐里的新品
    else if(tableView == self.nTableView)
    {
       ListdetailTableViewCell *cell =[tableView dequeueReusableCellWithIdentifier:@"newcell" forIndexPath:indexPath];
       ShipuModel *m = _nArr[indexPath.row];
        cell.title.text = m.title;
        cell.detail.text = m.des;
        [cell.imgView sd_setImageWithURL:[NSURL URLWithString:m.image]];
        return cell;
    }
    //专题
    else if (tableView == self.zhuantiTableview)
    {
        Head_2TableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"zhuanticell" forIndexPath:indexPath];
          ShipuModel *m = _zArr[indexPath.row];
         cell.name.text = m.title;
        [cell.img sd_setImageWithURL:[NSURL URLWithString:m.image]];
        cell.content.text = m.des;
        return cell;
        
    }//排行榜
    else if(tableView == self.hotFoodTableview)
    {
        SancanTableViewCell *cell =[tableView dequeueReusableCellWithIdentifier:@"foodcell" forIndexPath:indexPath];
        ShipuModel *m = _dataArr[indexPath.row];
        cell.name.text = m.title;
        cell.content.text = m.des;
        [cell.img sd_setImageWithURL:[NSURL URLWithString:m.image]];
        return cell;

    }
  else
        return nil;
    
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{   if(tableView == _nTableView)
{
    return 120 *HEIGHT;
}
else
    return 230 *HEIGHT;
}
#pragma mark -创建分段控制器
-(void)creatSeg
{
    self.seg = [[ UISegmentedControl alloc] initWithItems:@[@"热门",@"新品"]];
    self.seg.frame = CGRectMake(0,100 *HEIGHT, WIDTH*100, HEIGHT*30);
    self.navigationItem.titleView = self.seg;
    [self.seg release];
 self.seg .selectedSegmentIndex = 0;
    [self.seg addTarget:self action:@selector(segAction:) forControlEvents:UIControlEventValueChanged];
    
}
//分段控制器点击方法
-(void)segAction:(UISegmentedControl *)s
{
    if (s.selectedSegmentIndex == 0) {
        self.sv.contentOffset = CGPointMake(0, 0);
    }
    else{
        self.sv.contentOffset = CGPointMake(375 *WIDTH, 0);
    }
}

-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    
    self.seg.selectedSegmentIndex = scrollView.contentOffset.x /375*WIDTH;
}
//跳转页面 跳到三餐的详情页 共用一个
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    /**热门推荐的热门和热门排行榜*/
    if (tableView == _tableView || tableView == _hotFoodTableview) {
        
        ShikeViewController *shike = [[ ShikeViewController alloc] init];
        shike.model  = _dataArr[indexPath.row];
        [self .navigationController pushViewController:shike animated:YES];
        
    }
    /**热门推荐的新品*/
    else if (tableView == _nTableView)
    {
        
        ShikeViewController *newShike = [[ ShikeViewController alloc] init];
        newShike.model = _nArr[indexPath.row];
        [self.navigationController pushViewController:newShike animated:YES];
    }
    /**专题*/
    else if(tableView == _zhuantiTableview)
    {
        ZhuantiViewController *web = [[ ZhuantiViewController alloc] init];
        web.webmodel = _zArr[indexPath.row];
        [self .navigationController pushViewController:web animated:YES];
    }
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
